package com.uhu.code;

import com.uhu.code.help.ListNode;

/**
 * 160.相交链表
 * https://leetcode.cn/problems/intersection-of-two-linked-lists/?envType=study-plan-v2&envId=top-100-liked
 */
public class Solution160 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode l = headA;
        ListNode r = headB;
        while (l != r) {
            l = l.getNext();
            r = r.getNext();
            if (l == null && r == null) {
                break;
            }
            if (l == null) {
                l = headA;
            }
            if (r == null) {
                r = headB;
            }
        }
        return l;
    }
}
